<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <style>
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background-color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        fieldset {
            background-color: #f9f9f9;
            border: 2px solid #ddd;
            border-radius: 5px;
            padding: 20px;
            width: 400px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        legend {
            font-size: 1.2em;
            font-weight: bold;
            color: #333;
            padding: 0 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #663399;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1em;
            width: 100%;
        }
        button:hover {
            background-color: #5a2d8a;
        }
        .error {
            color: red;
            font-size: 0.9em;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <form id="loginForm">
        <fieldset>
            <legend>用户登录</legend>
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username">
                <div id="usernameError" class="error"></div>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password">
                <div id="passwordError" class="error"></div>
            </div>
            <button type="submit">登录</button>
        </fieldset>
    </form>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const form = document.getElementById('loginForm');
            const usernameInput = document.getElementById('username');
            const passwordInput = document.getElementById('password');
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');

            form.addEventListener('submit', function(event) {
                event.preventDefault(); // 阻止表单默认提交行为

                // 重置错误信息
                usernameError.style.display = 'none';
                passwordError.style.display = 'none';

                let isValid = true;

                // 验证用户名
                if (!usernameInput.value.trim()) {
                    usernameError.textContent = '用户名不能为空';
                    usernameError.style.display = 'block';
                    usernameInput.focus();
                    isValid = false;
                } else if (usernameInput.value.length < 8 || usernameInput.value.length > 20) {
                    usernameError.textContent = '用户名长度必须在8-20个字符之间';
                    usernameError.style.display = 'block';
                    usernameInput.focus();
                    isValid = false;
                }

                // 验证密码
                if (!passwordInput.value.trim()) {
                    passwordError.textContent = '密码不能为空';
                    passwordError.style.display = 'block';
                    if (isValid) passwordInput.focus(); // 只有用户名验证通过时才聚焦密码框
                    isValid = false;
                } else if (passwordInput.value.length < 8 || passwordInput.value.length > 20) {
                    passwordError.textContent = '密码长度必须在8-20个字符之间';
                    passwordError.style.display = 'block';
                    if (isValid) passwordInput.focus();
                    isValid = false;
                }

                // 如果所有验证通过，可以提交表单
                if (isValid) {
                    alert('登录成功！');
                    // form.submit(); // 实际应用中取消注释这行以提交表单
                }
            });
        });
    </script>
</body>
</html>